package com.zzb.util;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
public class AsyncDemo {
    @Resource
    RabbitTemplate rabbitTemplate;

    @Async
    public void asyncMethod() throws InterruptedException {
        for (int i = 1; i <= 10000; i++) {
            if (i % 2 == 0) {
                rabbitTemplate.convertAndSend("zzb.in_home.topic"
                        , "zzb.message.add"
                        , "线程" + Thread.currentThread().getName() + "发送的第" + i + "消息--" + System.currentTimeMillis());
            } else {
                rabbitTemplate.convertAndSend("zzb.in_home.topic"
                        , "zzb.message1.add"
                        , "线程" + Thread.currentThread().getName() + "发送的第" + i + "消息--" + System.currentTimeMillis());
            }
        }
        //System.out.println(Thread.currentThread().getName());
    }

    public void synMethod() {
        System.out.println(Thread.currentThread().getName() + "AsyncDemo····synMethod" + new Date());
    }
}